package com.agileai.portal.controller.base;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.base.PtPageTemptManage;
import com.agileai.portal.portlets.PortletCacheManager;

/**
 * generated by miscdp
 */
public class PtPageTemptContentHandler extends SimpleHandler{
	
	public PtPageTemptContentHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		String contentId = param.get("contentId");
		DataParam queryParam = new DataParam("TEMPT_ID",contentId);
		PtPageTemptManage ptStaticDataManage = this.lookupService(PtPageTemptManage.class);
		DataRow record = ptStaticDataManage.getRecord(queryParam);
		this.setAttributes(record);
		return new LocalRenderer(getPage());
	}
	
	@PageAction
	public ViewRenderer retrieveContent(DataParam param){
		String contentId = param.get("contentId");
		DataParam queryParam = new DataParam("TEMPT_ID",contentId);
		PtPageTemptManage ptStaticDataManage = this.lookupService(PtPageTemptManage.class);
		DataRow record = ptStaticDataManage.getRecord(queryParam);
		String responseText = record.stringValue("TEMPT_CONTENT");
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer saveContent(DataParam param){
		try {
			PtPageTemptManage ptStaticDataManage = this.lookupService(PtPageTemptManage.class);
			ptStaticDataManage.updateContent(param);
			String contentId = param.get("TEMPT_ID");
			String urlPrefix = PortletCacheManager.buildDataURLPefix(request);
			String tempateURL = urlPrefix+"/resource?PageTemptProvider&actionType=retrieveContent&contentId="+contentId;
			String shortURL = "/portal/resource?PageTemptProvider&actionType=retrieveContent&contentId="+contentId;
			PortletCacheManager.getOnly().removeCache(shortURL);
			PortletCacheManager.getOnly().removeCache(contentId);
			PortletCacheManager.getOnly().removeCache(tempateURL);
			return new AjaxRenderer(SUCCESS);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
			return new AjaxRenderer(FAIL);
		}
	}
}